package com.wzq.animal.back.service.impl;

import com.wzq.animal.back.service.SendMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;


@Service
public class SendMailServiceImpl implements SendMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    //发件人
    private String from = "wangzhiqiang1024@163.com";
    //private String from = "wangzhiqiang1024@163.com";
    //标题
    private String title = "管理员注册反馈";
    //正文（提交注册请求时反馈）
    public static String contentApplying = "欢迎您注册<b style='color:green'>陕西省珍稀动物网</b>的管理员！" +
            "    您的申请信息已经被网站接收，请您耐心等待，申请结果将在<b style='color:red'>5个工作日内</b>发送至您的邮箱，" +
            "感谢您对本网站的支持！<br>" +
            "有问题请联系负责人<b>王志强</b>或直接回复本邮箱。" +
            "电话：<b>19522433254</b><br>" +
            "<img src='https://sxanimals.oss-cn-hangzhou.aliyuncs.com/animalsImg/%E6%B0%B4%E7%8D%AD.jpeg' style='width:500px;height:300px' />";
    public static String contentSucceed = "<b style='color:red'>恭喜您通过</b><b style='color:green'>陕西省珍稀动物网</b>的申请，您现在可以使用之前注册的账号进行登录！\n" +
            "    欢迎您成为本网站管理员，让我们共同维护属于我们的网站，为提高陕西省珍稀动物的知名度做出一份贡献！<br>" +
            "有问题请联系负责人<b>王志强</b>或直接回复本邮箱。" +
            "电话：<b>19522433254</b><br>" +
            "<img src='https://sxanimals.oss-cn-hangzhou.aliyuncs.com/animalsImg/%E5%A4%A7%E7%86%8A%E7%8C%AB.jpeg' style='width:500px;height:300px' />";
    public static String contentFailed = "您的申请信息我们已经查看，很遗憾不能与您共同维护本网站，您暂时无法成为网站管理员，您的信息网站已经收录，如后期有机会，" +
            "我们会争取跟您合作，共同维护网站，感谢您对网站的支持！<br>" +
            "有问题请联系负责人<b>王志强</b>或直接回复本邮箱。" +
            "电话：<b>19522433254</b><br>" +
            "<img src='https://sxanimals.oss-cn-hangzhou.aliyuncs.com/animalsImg/%E5%B0%8F%E7%86%8A%E7%8C%AB.jpeg' />";
    public static String resetPassword = "请在<b style='color:red'>5分钟</b>之内完成验证，为了您的账号安全，请勿把本验证码泄露给其他人。<br>" +
            "有问题请联系负责人<b>王志强</b>或直接回复本邮箱。" +
            "电话：<b>19522433254</b><br>" +
            "<img src='https://sxanimals.oss-cn-hangzhou.aliyuncs.com/other/538876ca662e435985510a8d841b7770猞猁.jpeg' />";
    @Override
    public void sendMail(String username,String to,String content) {
        //发送带附件的邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        //不带附件的构造方法
        //MimeMessageHelper helper = new MimeMessageHelper(message);
        //带附件的构造方法
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        String contentPrefix = "亲爱的" + username + ":<br>";
        content = contentPrefix + content;
        try {
            helper.setFrom(from + "(陕西省珍稀动物网)");
            helper.setTo(to);
            helper.setSubject(title);
            //设置支持邮件解析html
            helper.setText(content,true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(message);
    }

    @Override
    public void sendCode(String username, String to, String content, String code) {
        //发送带附件的邮件
        MimeMessage message = javaMailSender.createMimeMessage();
        //不带附件的构造方法
        //MimeMessageHelper helper = new MimeMessageHelper(message);
        //带附件的构造方法
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message,true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        String contentPrefix = "亲爱的" + username + ":<br>";
        String codeStr = "您的验证码是<b style='color:blue'>" + code + "</b>";
        content = contentPrefix + codeStr + content;
        try {
            helper.setFrom(from + "(陕西省珍稀动物网)");
            helper.setTo(to);
            helper.setSubject(title);
            //设置支持邮件解析html
            helper.setText(content,true);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        javaMailSender.send(message);
    }
}
